<script lang="ts" setup>
import { onMounted,ref,reactive } from 'vue';
import {appWindow} from '@tauri-apps/api/window'
import { isRegistered,register } from '@tauri-apps/api/globalShortcut';
import { gettime } from './../function/index'
import { clipboard } from './../store/index'
const clipStore = clipboard();
import {writeText,readText,readImage,writeImage,} from "tauri-plugin-clipboard-api";
onMounted(async ()=>{
    document.getElementById("titlebar-minimize")?.addEventListener('click',function(){
      console.log(1)
        appWindow.minimize();
    })    

    document.getElementById("titlebar-maximize")?.addEventListener("click",function(){
        appWindow.toggleMaximize()
    })

    document.getElementById("titlebar-close")?.addEventListener("click",function(){
        appWindow.hide();
    })
    
    setInterval(async function(){
      let textcopy:string=await readText();
      let textfilter = clipStore.clipboard.filter((item:any)=>{
        return item.content==textcopy
      });

      if(textfilter.length==0){
        clipStore.clipboard.push({
          title:gettime(),
          content:textcopy
        });
      }
    },500)
})

const copy =async function(content:string){
  await writeText(content);
}

</script>

<template>
 <div data-tauri-drag-region class="titlebar">
    <div data-tauri-drag-region style="display: flex;align-items: center;justify-content: start;width: calc(100vw - 105px);padding-left: 20px;box-sizing: border-box;">
      剪贴板
    </div>
    <!-- 窗口操作 -->
    <div class="titlebar-button" id="titlebar-minimize">
      <img class="title-icon" src="/icon/zuixiaohua-1.png"
      alt="最小化"
      />
    </div>
    <div class="titlebar-button" id="titlebar-maximize">
      <img class="title-icon" src="/icon/zuidahua-1.png"
      alt="最大化"
      />
    </div>
    <div class="titlebar-button" id="titlebar-close">
      <img class="title-icon" src="/icon/guanbi1.png" alt="关闭" />
    </div>
  </div>
  <div style="height: 40px;"></div>
  <div class="clipboard">
    <div class="clipboard-item" v-for="item in [...clipStore.clipboard].reverse()" :key="item.title">
      <div class="title" style="width: 100%;">
        {{ item.title }}
        <div style="float: right;text-align: right;">
          <img src="/icon/chuangkou.png" style="width: 15px;display: inline-block;" @click="copy(item.content)" />
        </div>
      </div>
      {{ item.content }}
    </div>
  </div>
</template>

<style scoped>
.title-icon {
  width:30px;
  height:30px;
  padding: 5px;
  box-sizing: border-box;
}

.titlebar {
  width: 100vw;
  height: 30px;
  user-select: none;
  display: flex;
  z-index: 99;
  flex-direction: row;
  justify-content: end;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: gray;
}
.titlebar-button {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  width: 35px;
  height: 30px;
}
.titlebar-button:hover {
  background: #5bbec3;
}
.clipboard {
  width: 100vw;
  height: calc(100vh - 30px);
  padding-left: 10px;
  padding-right: 10px;
  padding-bottom: 10px;
  box-sizing: border-box;
  overflow-x:hidden;
}
.clipboard-item{
  width: calc(100vw - 20px );
  height: auto;
  background-color: gray;
  margin-bottom: 10px;
  cursor: pointer;
  border-radius: 5px;
  padding: 10px;
  box-sizing: border-box;
  background: #FFAFDF;
}


</style>